{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pyecharts\n",
      "  Downloading pyecharts-2.0.4-py3-none-any.whl (147 kB)\n",
      "Requirement already satisfied: jinja2 in c:\\program files\\anaconda3\\lib\\site-packages (from pyecharts) (2.11.2)\n",
      "Collecting prettytable\n",
      "  Downloading prettytable-3.9.0-py3-none-any.whl (27 kB)\n",
      "Collecting simplejson\n",
      "  Downloading simplejson-3.19.2-cp38-cp38-win_amd64.whl (75 kB)\n",
      "Requirement already satisfied: MarkupSafe>=0.23 in c:\\program files\\anaconda3\\lib\\site-packages (from jinja2->pyecharts) (1.1.1)\n",
      "Requirement already satisfied: wcwidth in c:\\program files\\anaconda3\\lib\\site-packages (from prettytable->pyecharts) (0.2.5)\n",
      "Installing collected packages: prettytable, simplejson, pyecharts\n",
      "Successfully installed prettytable-3.9.0 pyecharts-2.0.4 simplejson-3.19.2\n"
     ]
    }
   ],
   "source": [
    "!pip install pyecharts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Flask 数据可视化详解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "   WARNING: This is a development server. Do not use it in a production deployment.\n",
      "   Use a production WSGI server instead.\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [22/Nov/2023 20:05:44] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask # python 后端与 前端连接的一个框架\n",
    "from jinja2 import Markup, Environment, FileSystemLoader # Jinja2 前端页面展示的框架\n",
    "from pyecharts.globals import CurrentConfig # pyecharts : A Python Echarts Plotting Library\n",
    "\n",
    "\n",
    "CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader(\"./templates\"))\n",
    "# pyecharts 模板 需要在github下载templates的模板文件\n",
    "\n",
    "from pyecharts import options as opts\n",
    "# options 图表的配置项 graph\n",
    "\n",
    "from pyecharts.charts import Bar\n",
    "# Bar 柱状图 pyecharts 封装好的图形，可参见 gallery 示例\n",
    "\n",
    "from pyecharts.charts import Geo\n",
    "from pyecharts.globals import ChartType, SymbolType\n",
    "\n",
    "app = Flask(__name__, static_folder=\"templates\")\n",
    "\n",
    "\n",
    "# 1. 找到合适的可视化图表，找到gallery示例代码，测试一下示例代码，\n",
    "# 2. 准备数据分析结果的参数，替换示例代码的参数\n",
    "# 3. 创建制作图的函数\n",
    "def bar_base() -> Bar:\n",
    "    c = (\n",
    "        Bar()\n",
    "        .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "        .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "        .add_yaxis(\"商家B\", [15, 25, 16, 55, 48, 8])\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Bar-基本示例\", subtitle=\"我是副标题\"))\n",
    "    )\n",
    "    return c\n",
    "\n",
    "def geo_lines():\n",
    "    c = (\n",
    "        Geo()\n",
    "        .add_schema(maptype=\"china\")\n",
    "        .add(\n",
    "            \"\",\n",
    "            [(\"广州\", 55), (\"北京\", 66), (\"杭州\", 77), (\"重庆\", 88)],\n",
    "            type_=ChartType.EFFECT_SCATTER,\n",
    "            color=\"white\",\n",
    "        )\n",
    "        .add(\n",
    "            \"geo\",\n",
    "            [(\"广州\", \"上海\"), (\"广州\", \"北京\"), (\"广州\", \"杭州\"), (\"广州\", \"重庆\")],\n",
    "            type_=ChartType.LINES,\n",
    "            effect_opts=opts.EffectOpts(\n",
    "                symbol=SymbolType.ARROW, symbol_size=6, color=\"blue\"\n",
    "            ),\n",
    "            linestyle_opts=opts.LineStyleOpts(curve=0.2),\n",
    "        )\n",
    "        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Geo-Lines\"))\n",
    "    )\n",
    "    return c\n",
    "\n",
    "# 4. 将可视化图形通过markup方法，绘制在HTML的页面上\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    c = geo_lines()\n",
    "    return Markup(c.render_embed())\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Flask 前后端分离"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "   WARNING: This is a development server. Do not use it in a production deployment.\n",
      "   Use a production WSGI server instead.\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [22/Nov/2023 20:46:50] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n",
      "127.0.0.1 - - [22/Nov/2023 20:46:50] \"\u001b[37mGET /barChart HTTP/1.1\u001b[0m\" 200 -\n",
      "127.0.0.1 - - [22/Nov/2023 20:46:50] \"\u001b[37mGET /geo_lines HTTP/1.1\u001b[0m\" 200 -\n"
     ]
    }
   ],
   "source": [
    "from random import randrange\n",
    "\n",
    "from flask import Flask, render_template\n",
    "\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts.faker import Faker\n",
    "from pyecharts.globals import ChartType\n",
    "\n",
    "app = Flask(__name__, static_folder=\"templates\")\n",
    "\n",
    "\n",
    "def bar_base() -> Bar:\n",
    "    c = (\n",
    "        Bar()\n",
    "        .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "        .add_yaxis(\"商家A\", [randrange(0, 100) for _ in range(6)])\n",
    "        .add_yaxis(\"商家B\", [randrange(0, 100) for _ in range(6)])\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Bar-基本示例\", subtitle=\"我是副标题\"))\n",
    "    )\n",
    "    return c\n",
    "\n",
    "def geo_lines():\n",
    "    c = (\n",
    "        Geo()\n",
    "        .add_schema(maptype=\"广东\")\n",
    "        .add(\n",
    "            \"geo\",\n",
    "            [list(z) for z in zip(Faker.guangdong_city, Faker.values())],\n",
    "            type_=ChartType.HEATMAP,\n",
    "        )\n",
    "        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "        .set_global_opts(\n",
    "            visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title=\"Geo-广东地图\")\n",
    "        )\n",
    "    )\n",
    "    return c\n",
    "\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    return render_template(\"index.html\")\n",
    "\n",
    "\n",
    "@app.route(\"/barChart\")\n",
    "def get_bar_chart():\n",
    "    c = bar_base()\n",
    "    return c.dump_options_with_quotes()\n",
    "\n",
    "@app.route(\"/geo_lines\")\n",
    "def get_geo_lines():\n",
    "    c = geo_lines()\n",
    "    return c.dump_options_with_quotes()\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
